//
//  selectAllGroupViewController.m
//  SouthChina
//
//  Created by zorro on 15/7/3.
//  Copyright (c) 2015年 科创. All rights reserved.
//

#import "selectAllGroupViewController.h"
#import "SDRefresh.h"
#import "selectTableViewController.h"
#import "selectMemberViewController.h"
@interface selectAllGroupViewController ()<UITableViewDataSource,UITableViewDelegate,UISearchBarDelegate >
{
    SDRefreshFooterView * refreshFooterView;
    NSMutableArray * allGroup;
    NSString * groupType;
    NSString * searchName;
    int pageNo;
    int pageSize;
}
@end

@implementation selectAllGroupViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    /*记得初始化成员变量*/
    groupType = @"2";//搜索行政群
    pageNo = 1;
    pageSize= 10;
    searchName = [NSString stringWithFormat:@""];
    allGroup = [[NSMutableArray alloc] init];
    
    [self addTitleBarWithBackBtn:@"选择上级群" needReturn:TRUE];
    
    UIView * viewBody = [[[NSBundle mainBundle] loadNibNamed:@"selectAllGroupViewBody" owner:self options:nil] objectAtIndex:0];
    
    viewBody.frame = (CGRect){0, TITLE_BAR_HEGIHT+YNEW,SCREEN_WIDTH, SCREEN_HEIGHT-TITLE_BAR_HEGIHT-YNEW};
    [self.view addSubview:viewBody];
    
    
    _searchGroup.showsCancelButton=NO;
    _searchGroup.barStyle=UIBarStyleDefault;
    _searchGroup.placeholder = @"搜索";
    _searchGroup.delegate = self;
    
   
    _tabGroup.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
    
    
    _tabGroup.delegate = self;
    _tabGroup.dataSource = self;
    _tabGroup.bounces=NO;
    
    [self setupFooter];
    [self footerRefresh];
    

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar
{
    //重新设置新数据
    [allGroup removeAllObjects];
    pageNo = 1;
    searchName = searchBar.text;
    [self refreshTableView:searchName];
}

- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText{
    
    if([searchText isEqualToString:@""])
    {
        [self searchBarSearchButtonClicked:searchBar];
    }
    
}

- (void)setupFooter
{
    refreshFooterView = [SDRefreshFooterView refreshView];
    
    [refreshFooterView addToScrollView:_tabGroup];
    
    [refreshFooterView addTarget:self refreshAction:@selector(footerRefresh)];
    
}
- (void)dealloc
{
    [refreshFooterView removeFromSuperview];
}

- (void)footerRefresh
{
    //加载新数据
    [self refreshTableView:searchName];
    [refreshFooterView endRefreshing];
}

-(void) refreshTableView:(NSString*) key
{
    NSDictionary *map = @{@"groupType":groupType,@"groupName":key,@"pageNo": [NSString stringWithFormat:@"%d",pageNo],@"pageSize":[NSString stringWithFormat:@"%d",pageSize]};
    
    //如果上次现实加载提示窗还没清除，又进行了调用，界面出现多个提示框，清理时会出异常，导致无法全部清理干净，界面僵死
    
    [MBProgressHUD hideHUDForView:self.view animated:YES];
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    
    [DE serverCall:@"searchGrp" params:map callback:^(BOOL success, int code, NSString *desc, NSDictionary *data,NSDictionary*param) {
        
        [MBProgressHUD hideHUDForView:self.view animated:YES];
        if(success)
        {
            pageNo++; //查询成功，进行翻页
            NSArray *array =  [data objectForKey:@"grps"];
            [allGroup addObjectsFromArray:array];
            [_tabGroup reloadData];
        }
        else{
            [self showAlerHUD:@"获取群列表失败"];
        }
    }];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return allGroup.count+1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }
    
    if(indexPath.row == 0)
    {
        cell.textLabel.text = @"暂时不选择挂靠的上级群";
    }
    else
    {
        cell.textLabel.text = [[allGroup objectAtIndex:indexPath.row-1] objectForKey:@"groupName" ];
        [cell.imageView setImage:[UIImage imageNamed:@"group"]];
    }
    
    
    return  cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 50;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    //选种的单元格信息,返回被挂靠的群
    if(indexPath.row == 0)
    {
        [_superGroup removeAllObjects];
    }
    else
    {
        [_superGroup addEntriesFromDictionary:[allGroup objectAtIndex:indexPath.row-1]];
    }
    [super btnBack];
}

@end
